<?php
namespace Badmins\Controller;
use Think\Controller;
class MotionController extends Controller {
	/**
	 * 限时抢购
	 */
    public function index(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$where = array();
    	$name = I('name');
    	if(!empty($name)){
    		$where['gb.name'] = array('like',"%{$name}%");
    	}
    	$start_time = I('start_time');
    	if($start_time){
    		$where['gb.start_time'] = array('elt',$start_time);
    	}
    	$end_time = I('end_time');
    	if($end_time){
    		$where['gb.end_time'] = array('egt',$end_time." 23:59:59");
    	}
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$db = M('group_buying as gb');
    	$where['gb.type'] = 0;
    	$count = $db->where($where)->count();
    	$firstrow = ($p-1)*$listrow;
    	$join = "left join __PRODUCTS__ as p on gb.goods_id = p.id left join __GOODS__ as g on g.id = p.goods_id";
    	$list = lists('group_buying as gb',$join,$where,"gb.*,g.name as goods_name","id desc",$firstrow,$listrow,2);
    	if(!empty($list)){
    		$today = date("Y-m-d H:i:s");
    		foreach ($list as $i=>$va){
    			if($va['start_time']>$today){
    				//未开始
    				$list[$i]['ck'] = 0;
    			}elseif ($va['end_time']<$today){
    				//已结束
    				$list[$i]['ck'] = 1;
    			}else{
    				$list[$i]['ck'] = 2;
    			}
    		}
    	}
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	$this->display();
    }
    /**
     * 团购
     */
    public function groupbuy(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$where = array();
    	$name = I('name');
    	if(!empty($name)){
    		$where['gb.name'] = array('like',"%{$name}%");
    	}
    	$start_time = I('start_time');
    	if($start_time){
    		$where['gb.start_time'] = array('elt',$start_time);
    	}
    	$end_time = I('end_time');
    	if($end_time){
    		$where['gb.end_time'] = array('egt',$end_time." 23:59:59");
    	}
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$db = M('group_buying as gb');
    	$where['gb.type'] = 1;
    	$count = $db->where($where)->count();
    	$firstrow = ($p-1)*$listrow;
    	$join = "left join __PRODUCTS__ as p on gb.goods_id = p.id left join __GOODS__ as g on g.id = p.goods_id";
    	$list = lists('group_buying as gb',$join,$where,"gb.*,g.name as goods_name","id desc",$firstrow,$listrow,2);
    	if(!empty($list)){
    		$today = date("Y-m-d H:i:s");
    		foreach ($list as $i=>$va){
    			if($va['start_time']>$today){
    				//未开始
    				$list[$i]['ck'] = 0;
    			}elseif ($va['end_time']<$today){
    				//已结束
    				$list[$i]['ck'] = 1;
    			}else{
    				$list[$i]['ck'] = 2;
    			}
    		}
    	}
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	$this->display();
    }
    /**
     * 优惠券
     */
    public function coupon(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$where = array();
    	$name = I('name');
    	if(!empty($name)){
    		$where['name'] = array('like',"%{$name}%");
    	}
    	$start_time = I('start_time');
    	if($start_time){
    		$where['start_time'] = array('elt',$start_time);
    	}
    	$end_time = I('end_time');
    	if($end_time){
    		$where['end_time'] = array('egt',$end_time." 23:59:59");
    	}
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$db = M('ticket');
    	$count = $db->where($where)->count();
    	$firstrow = ($p-1)*$listrow;
    	$list = lists('ticket',"",$where,"*","id desc",$firstrow,$listrow,2);
    	if(!empty($list)){
    		$today = date("Y-m-d H:i:s");
    		foreach ($list as $i=>$va){
    			if($va['start_time']>$today){
    				//未开始
    				$list[$i]['ck'] = 0;
    			}elseif ($va['end_time']<$today){
    				//已结束
    				$list[$i]['ck'] = 1;
    			}else{
    				$list[$i]['ck'] = 2;
    			}
    		}
    	}
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	$this->display();
    }
    /**
     * 商品促销
     */
    public function prom(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$where = array();
    	$name = I('name');
    	if(!empty($name)){
    		$where['name'] = array('like',"%{$name}%");
    	}
    	$start_time = I('start_time');
    	if($start_time){
    		$where['start_time'] = array('elt',$start_time);
    	}
    	$end_time = I('end_time');
    	if($end_time){
    		$where['end_time'] = array('egt',$end_time." 23:59:59");
    	}
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$db = M('promotion');
    	$where['type'] = 0;
    	$count = $db->where($where)->count();
    	$firstrow = ($p-1)*$listrow;
    	$list = lists('promotion',"",$where,"*","id desc",$firstrow,$listrow,2);
    	if(!empty($list)){
    		$today = date("Y-m-d H:i:s");
    		foreach ($list as $i=>$va){
    			if($va['start_time']>$today){
    				//未开始
    				$list[$i]['ck'] = 0;
    			}elseif ($va['end_time']<$today){
    				//已结束
    				$list[$i]['ck'] = 1;
    			}else{
    				$list[$i]['ck'] = 2;
    			}
    			switch ($va['award_type']){
    				case '1':
    					$list[$i]['typename'] = "打折";
    				break;
    				case '2':
    					$list[$i]['typename'] = "减价优惠";
    				break;
    				case '3':
    					$list[$i]['typename'] = "固定金额";
    				break;
    				case '4':
    					$list[$i]['typename'] = "买就送优惠券";
    				break;
    			}
    			if($va['user_group'] == "all"){
    				$list[$i]['groupname'] = "全部会员";
    			}else{
    				$group[$i] = M('user_group')->where("id in (".$va['user_group'].")")->field("name")->order("id desc")->select();
    				foreach ($group[$i] as $k=>$vv){
    					$grouplist[$i][] = $vv['name'];
    				}
    				$list[$i]['groupname'] = join(",",$grouplist[$i]);
    			}
    		}
    	}
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	$this->display();
    }
    /**
     * 订单促销
     */
    public function orderprom(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$where = array();
    	$name = I('name');
    	if(!empty($name)){
    		$where['name'] = array('like',"%{$name}%");
    	}
    	$start_time = I('start_time');
    	if($start_time){
    		$where['start_time'] = array('elt',$start_time);
    	}
    	$end_time = I('end_time');
    	if($end_time){
    		$where['end_time'] = array('egt',$end_time." 23:59:59");
    	}
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$db = M('promotion');
    	$where['type'] = 1;
    	$count = $db->where($where)->count();
    	$firstrow = ($p-1)*$listrow;
    	$list = lists('promotion',"",$where,"*","id desc",$firstrow,$listrow,2);
    	if(!empty($list)){
    		$today = date("Y-m-d H:i:s");
    		foreach ($list as $i=>$va){
    			if($va['start_time']>$today){
    				//未开始
    				$list[$i]['ck'] = 0;
    			}elseif ($va['end_time']<$today){
    				//已结束
    				$list[$i]['ck'] = 1;
    			}else{
    				$list[$i]['ck'] = 2;
    			}
    			switch ($va['award_type']){
    				case '1':
    					$list[$i]['typename'] = "打折";
    					break;
    				case '2':
    					$list[$i]['typename'] = "减价优惠";
    					break;
    				case '3':
    					$list[$i]['typename'] = "满额送积分";
    					break;
    				case '4':
    					$list[$i]['typename'] = "买就送优惠券";
    					break;
    				case '5':
    					$list[$i]['typename'] = "免运费";
    				break;
    			}
    			if($va['user_group'] == "all"){
    				$list[$i]['groupname'] = "全部会员";
    			}else{
    				$group[$i] = M('user_group')->where("id in (".$va['user_group'].")")->field("name")->order("id desc")->select();
    				foreach ($group[$i] as $k=>$vv){
    					$grouplist[$i][] = $vv['name'];
    				}
    				$list[$i]['groupname'] = join(",",$grouplist[$i]);
    			}
    		}
    	}
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	$this->display();
    }
}


